package org.tiger.thymeleaf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.tiger.thymeleaf.util.VerifyImageUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author lwl
 * @date 2019/9/12 10:06
 */
@Controller
@RequestMapping("/captcha")
public class CapthcaController {

  private static final String IMG_PATH = "/static/targets";

  private static final String TEMP_IMG_PATH = "/static/templates/1.png";

  private static final Long IMG_CACHE_EX_TIME = 120L;

  @RequestMapping("/index")
  public String index() {

    return "/login/show";
  }

  /**
   * @param @return 参数说明
   * @return BaseRestResult 返回类型 @Description: 生成滑块拼图验证码
   */
  @RequestMapping(value = "/getImageVerifyCode")
  @ResponseBody
  public Map<String, Object> getImageVerifyCode() throws Exception {
    // 读取图库目录
    File imgCatalog = new File(getClass().getResource(IMG_PATH).getPath());
    File[] files = imgCatalog.listFiles();

    int randNum = new Random().nextInt(files.length);
    File targetFile = files[randNum];
    File tempImgFile = new File(getClass().getResource(TEMP_IMG_PATH).getPath());

    // 根据模板裁剪图片
    Map<String, Object> resultMap = VerifyImageUtil.pictureTemplatesCut(tempImgFile, targetFile);
    int xWidth = (int) resultMap.get("xWidth");
    // sessionId 为key，value滑动距离X轴，缓存120秒
    // redisCacheHelper.set(session.getId(), xWidth, IMG_CACHE_EX_TIME);

    // 移除map的滑动距离，不返回给前端
    resultMap.remove("xWidth");
    // resultMap.put(ApiConstants.ERR_CODE, ApiConstants.SUCCESS_CODE_0);
    // resultMap.put(ApiConstants.ERR_MSG, ApiConstants.SUCCESS_MSG);
    return resultMap;
  }

  /**
   * 校验滑块拼图验证码
   *
   * @param moveLength 移动距离
   * @return BaseRestResult 返回类型 @Description: 生成图形验证码
   */
  @RequestMapping(
      value = "/verifyImageCode",
      method = RequestMethod.GET,
      produces = {"application/json;charset=UTF-8"})
  @ResponseBody
  public Map<String, Object> verifyImageCode(
      @RequestParam(value = "moveLength", required = false) String moveLength) {
    Double dMoveLength = Double.valueOf(moveLength);
    Map<String, Object> resultMap = new HashMap<>();
    try {
      // Object xWidth = redisCacheHelper.get(session.getId());
      Object xWidth = 10;
      if (xWidth == null) {
        resultMap.put("errcode", 1);
        resultMap.put("errmsg", "验证过期，请重试");
        return resultMap;
      }
      ;
      if (Math.abs((int) xWidth - dMoveLength) > 10) {
        resultMap.put("errcode", 1);
        resultMap.put("errmsg", "验证不通过");
      } else {
        resultMap.put("errcode", 0);
        resultMap.put("errmsg", "验证通过");
      }
    } catch (Exception e) {
      // throw new EsServiceException(e.getMessage());
    } finally {
      // redisCacheHelper.del("xWidth");
    }
    return resultMap;
  }
}
